<?php
global $AppUI, $company_id, $obj, $m;

// Check permission
// Get login object
$uid = $AppUI->user_id;
// Check permission
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canEdit = $perms->checkModuleItem($m, 'edit', $company_id);

require_once $AppUI->getModuleClass('contacts');
$q = new DBQuery;
$q->addTable('contacts', 'c');
$q->addQuery('contact_id, contact_email, dept_name');
$q->addQuery('CONCAT(contact_first_name, " ", contact_last_name) AS contact_name');
$q->addJoin('departments', 'd', 'contact_department = dept_id');
$q->addWhere('contact_company = '.$obj->company_id);
$q->addOrder('contact_last_name');

$total = 0;
$s = '';
if (!($rows = $q->loadList())) {
	$s .= '<tr class="second"><td colspan="3">'.$AppUI->_('No data available').'<br />'.$AppUI->getMsg().'</td></tr>';
} else {
	$total = count($rows);
	foreach ($rows as $row) {
		$s .= '<tr class="second">';
		if($canEdit) $s .= '<td><img class="clickable" src="images/edit.gif" onclick="doEditCard('.$row['contact_id'].')"></td>';		
		else $s .= '<td>&nbsp;</td>';
		$s .= '<td><a href="javascript:doViewCard('.$row['contact_id'].')">'.$row['contact_name'].'</a></td>';
		$s .= '<td><a href="mailto:'.$row['contact_email'] .'">' .$row['contact_email'] .'</a></td>';
		$s .= '<td>'.$row['dept_name'] .'</td>';
		$s .= '</tr>';
	}
}

$AppUI->getModuleJS('contacts');
?>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td class="status">Total:</td>
	<td><?=$total?>&nbsp;contacts</td>
	<td width="100%" align="right">
	<?php
		if($canEdit){
			showButton($AppUI->_('New contact'), 'js:doEditCard(0, \'&company_id='.$company_id.'\')');
		}
	?>
	</td>
</tr>
</table>
<table width="100%" border=0 cellpadding="2" cellspacing="0" class="list">
<tr>
	<th width="16">&nbsp;</td>
	<th><?=$AppUI->_( 'Name' )?></td>
	<th><?=$AppUI->_( 'Email' )?></td>
	<th><?=$AppUI->_( 'Department' )?></td>
</tr>
<?
	if (!$total)
		showEmptyRow(3, 'padding');
	else
		echo $s;
?>
</table>
